package com.idroc.rpc;

import java.util.UUID;

import org.apache.mina.common.ByteBuffer;

import com.idroc.message.AbstractMessage;

/**
 *  RPC message
 * @author www.tianya.cn
 * @creator 詹都林 (zhandl@hainan.net) 
 * Aug 12, 2009 5:47:00 PM
 * @memo:
 */
public class RPCMessage extends AbstractMessage {

	private long reqMSB = 0;
	private long reqLSB = 0;
	
	private int serviceId = 0;
	private ByteBuffer reqData = null;

	@Override
	protected void decodeMessage(ByteBuffer in) { 
		
		reqLSB = in.getLong();
		reqMSB = in.getLong();
		serviceId = in.getInt();
		reqData = in.slice(); 
	}

	@Override
	protected void encodeMessage(ByteBuffer out) { 
		
		out.putLong(reqLSB);
		out.putLong(reqMSB);
		out.putInt(serviceId);
		reqData.flip();
		out.put(reqData);
	}
	
	public void setRequestId(UUID reqId) {
		
		this.reqLSB = reqId.getLeastSignificantBits();
		this.reqMSB = reqId.getMostSignificantBits();
	}
	
	public void setServiceId(int sid) {
		this.serviceId = sid;
	}
	
	public void setRequestData(ByteBuffer src) {
		this.reqData = src;
	}
	
	public UUID getRequestId() {
		
		UUID reqId = new UUID(this.reqMSB, this.reqLSB);
		return reqId;
	}
	
	public int getServiceId() {
		
		return serviceId;
	}
	
	public ByteBuffer getRequestData() {
		return this.reqData;
	}
	
	@Override
	public String toString() {
		
		StringBuilder sb = new StringBuilder();
		sb.append("RPCMessage[");
		sb.append("reqMSB:").append(reqMSB).append(",");
		sb.append("reqLSB:").append(reqLSB).append(",");
		sb.append("serviceId:").append(serviceId).append(","); 
		sb.append("reqData:{pos:").append(reqData.position()).append(",");
		sb.append("limit:").append(reqData.limit()).append(",");
		sb.append("hashCode:").append(reqData.hashCode()).append("}");		
		sb.append("]");
		return sb.toString();
	}
}
